package com.atguigu.yygh.cmn.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;

/** 
* @author 作者 Your-Name: 
* @version 创建时间：2021年4月12日 下午1:17:06 
* 类说明 
*/
@Api(tags ="数据字典列表")
@RestController
@RequestMapping(value = "/admin/cmn/dict")
//@CrossOrigin  //允许跨域访问
public class DictController {

	@Autowired
	private DictService dictService;
	
	//根据 dict_code value 进行查询 返回name
	@GetMapping("/getName/{dictCode}/{value}")
	public String getName(@PathVariable(value = "dictCode") String dictCode, @PathVariable(value = "value") String value){
		String dictName = dictService.getDictName(dictCode, value);
		return dictName;
	}
	
	//根据value 进行查询   返回name
	@GetMapping("/getName/{value}")
	public String getName( @PathVariable(value = "value") String value){
		String dictName = dictService.getDictName("", value);
		return dictName;
	}
	
	//根据dictCode 查询下级节点
	@GetMapping("/findByDictCode/{dictCode}")
	@ApiOperation("根据dictCode 查询下级节点")
	public Result findByDictCode(@PathVariable(value = "dictCode") String dictCode){
		List<Dict> dlist = dictService.findByDictCode(dictCode);
		return Result.ok(dlist);
	}
	
	//根据 数据 id 查询子数据列表   id = parent_id
	@GetMapping("/findChildData/{id}")
	@ApiOperation("根据数据id获取子数据列表")
	public Result findChildData(@PathVariable(value = "id") Long id){
		List<Dict> dlist = dictService.findChildData(id);
		return Result.ok(dlist);
	}
	
	//导出数据字典接口
	@GetMapping("/exportExcel")
	public void exportExcel(HttpServletResponse response){
		dictService.exportDictExcel(response);
	}
	
	//导入数据字典接口  MultipartFile 获取文件
    @PostMapping("/importExcel")
	public Result importExcel(MultipartFile file){
		dictService.importDictExcel(file);
		return Result.ok();
	}
	

}
